package com.svo.md;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.b510.bean.Person;
import com.b510.service.PersonService;
import com.svo.md.bean.InviteEntity;
import com.svo.md.bean.SecretEntity;
import com.svo.md.bean.UserEntity;
import com.svo.md.service.InviteService;
import com.svo.md.service.SecretService;
import com.svo.md.service.UserService;
import com.svo.util.MemcachedUtils;

public class SpringJDBCMd {
	
    public static void main(String[] args) throws MalformedURLException, IOException {
        ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
//        userAndSecret(act);
        invite(act);
        		
    }

	private static void invite(ApplicationContext act) {
		InviteService inviteService = (InviteService) act.getBean("inviteService");
	    String inviteJson = "";
		try {
			inviteJson = readJson();
			JSONArray array = new JSONArray(inviteJson);
			System.err.println("::"+array.length());
	        for (int i = 0; i < array.length(); i++) {
				JSONObject jo = array.getJSONObject(i);
				InviteEntity entity = new InviteEntity();
		        entity.setMainId(jo.optString("mainId"));
		        entity.setInviteId(jo.optString("inviteId"));
		        long time = jo.optLong("time");
		        if (time > 0) {
		        	entity.setTime(new Timestamp(time));
				}
		        inviteService.save(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	    
	}

	private static String readJson() throws FileNotFoundException {
		Scanner scanner = new Scanner(new File("d:/tmp/invite_json.txt"));
		StringBuilder sb = new StringBuilder();
		while (scanner.hasNextLine()) {
			String nextLine = scanner.nextLine();
			sb.append(nextLine);
		}
		scanner.close();
		return sb.toString();
	}

	private static void userAndSecret(ApplicationContext act) {
		UserService personService = (UserService) act.getBean("userService");
        String json = "";
        String emptyUserJson = "";
        json = emptyUserJson;
        JSONArray array = new JSONArray(json);
        for (int i = 0; i < array.length(); i++) {
			JSONObject jo = array.getJSONObject(i);
			UserEntity entity = new UserEntity();
	        entity.setDevId(jo.optString("devId"));
	        long startTime = jo.optLong("startTime");
	        if (startTime > 0) {
	        	entity.setStartTime(new Timestamp(startTime));
			}
	        long endTime = jo.optLong("endTime");
	        if (endTime > 0) {
	        	entity.setEndTime(new Timestamp(endTime));
			}
	        personService.save(entity);
		}
        SecretService secretService = (SecretService) act.getBean("secretService");
        for (int i = 0; i < array.length(); i++) {
			JSONObject jo = array.getJSONObject(i);
			SecretEntity entity = new SecretEntity();
			String secret_md5 = jo.optString("secret_md5");
			if (secret_md5.isEmpty()) {
				continue;
			}
			entity.setMd5(secret_md5);
			entity.setProxy_channel(jo.optString("proxy_channel"));
	        entity.setDevId(jo.optString("devId"));
	        entity.setStartTime(new Timestamp(jo.optLong("startTime")));
	        long endTime = jo.optLong("endTime");
	        if (endTime > 0) {
	        	entity.setEndTime(new Timestamp(endTime));
			}
	        secretService.save(entity);
		}
        /*entity = personService.getPerson("8cc84d79b4c71c274d8182d5245ca204");
        System.err.println(entity.getDevId()+","+entity.getStartTime());*/
	}
}